package cn.hf.mychat.controller;


import cn.hf.mychat.config.Result;
import cn.hf.mychat.entity.bo.RequestUser;
import cn.hf.mychat.entity.test.TestEntity;
import cn.hf.mychat.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ansi.AnsiOutput;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.sound.midi.Soundbank;
import javax.validation.Valid;

@RestController
@RequestMapping("/hf")
@RequiredArgsConstructor
public class UserController {

    public static final String ABC="1";

    private final UserService userService;

    private final ApplicationEventPublisher applicationEventPublisher;

    @Value("${text.aa}")
    private String aa;

    @Value("${test.password}")
    private Integer password;

    @Value("${panpan.name}")
    private String name;
    /**
     * 注册
     * @return 注册状态
     */
    @PostMapping("register")
    @ResponseBody
    public ResponseEntity<?> register(@RequestBody @Valid RequestUser requestUser) {
        //TODO 正则验证格式
        Integer count = this.userService.register(requestUser);
        if (count == 1 ){
            return ResponseEntity.ok(Result.success(1));
        }
        return ResponseEntity.ok(Result.error("注册失败"));
    }
    /**
     * 注册
     * @return 注册状态
     */
    @PostMapping("test")
    @ResponseBody
    public String register( ) {
        //TODO 正则验证格式
       String user= this.userService.getUser();
       return  user;
    }

    @PostMapping("listener")
    public String listener(@RequestBody TestEntity entity) throws InterruptedException {
      applicationEventPublisher.publishEvent(entity);

        System.out.println("00000000000000000000");
      /*  Thread.sleep(5000);*/
        System.out.println(entity);
      return "完成";
    }

    @GetMapping("async")
    public String async(){
        //执行一个异步的方法。即时返回数据前端
        System.out.println(aa);
        System.out.println(password);
        System.out.println(name);
        userService.async();
        return "完成";
    }
}
